package com.yzjs.localhost.api;

import com.yzjs.common.enumeration.RequestMethod;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.localhost.dictionaries.DealDictionary;
import com.yzjs.localhost.dto.TDataPersistenceAfterDTO;
import com.yzjs.localhost.entity.tDataLog.TDataLogEntity;
import com.yzjs.localhost.entity.tDataPersistenceAfter.TDataPersistenceAfterEntity;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.service.tDataLog.TDataLogService;
import com.yzjs.localhost.service.tDataPersistenceAfter.TDataPersistenceAfterService;
import com.yzjs.localhost.service.tDataPersistenceBeforce.TDataPersistenceBeforceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


@Api(tags = "东软接收报文入参")
@RestController
@RequestMapping("receive")
public class ReviceController {

    private static Logger logger = LogManager.getLogger(ReviceController.class.getName());


    @Autowired
    private TDataLogService tDataLogService;

    @Autowired
    private TDataPersistenceAfterService tDataPersistenceAfterService;

    @Autowired
    private TDataPersistenceBeforceService tDataPersistenceBeforceService;

    /**
     * 入库之前数据上传
     * @return
     */
    @ApiOperation(value="入库之前数据上传" ,httpMethod = RequestMethod.POST )
    @RequestMapping("/dataPersistenceBeforce")
    public TDataPersistenceBeforceEntity List(@RequestBody TDataPersistenceBeforceEntity tDataPersistenceBeforce) throws Exception {
        String json =JsonUtil.obj2json(tDataPersistenceBeforce);
        logger.info(json);

       tDataPersistenceBeforceService.insert(tDataPersistenceBeforce);

        return tDataPersistenceBeforce;
    }

    /**
     * 数据存储数据库后上传
     * @return
     */
    @ApiOperation(value="数据存储数据库后上传" ,httpMethod = RequestMethod.POST )
    @RequestMapping("/dataPersistenceAfter")
    public TDataPersistenceAfterDTO List(@RequestBody TDataPersistenceAfterDTO tDataPersistenceAfter) throws Exception {
        String json =JsonUtil.obj2json(tDataPersistenceAfter);
        logger.info(json);
        TDataPersistenceAfterEntity entity  = new TDataPersistenceAfterEntity();
        BeanUtils.copyProperties(tDataPersistenceAfter,entity);
        entity.setInfo(JsonUtil.obj2json(tDataPersistenceAfter.getInfo()));
        tDataPersistenceAfterService.insert(entity);
        return tDataPersistenceAfter;
    }

    /**
     * 接口日志
     * @return
     */
    @ApiOperation(value="接口日志" ,httpMethod = RequestMethod.POST )
    @RequestMapping("/dataLog")
    public TDataLogEntity List(@RequestBody TDataLogEntity tDataLog) throws Exception {
        String json =JsonUtil.obj2json(tDataLog);
        logger.info(json);
        tDataLog.setDealSign(DealDictionary.UNTREATED);
        tDataLogService.insert(tDataLog);
        return tDataLog;
    }

}
